from fastapi import APIRouter, Depends, Form, Request
from datetime import datetime
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_admin.service.ocr_config_service import OcrConfigService
from module_admin.entity.vo.ocr_config_vo import (
    DeleteOcrConfigModel,
    OcrConfigModel,
    OcrConfigPageQueryModel,
    AddOcrConfigModel,
    EditOcrConfigModel,
)
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


OcrConfigController = APIRouter(
    prefix="/tool/OcrConfig", dependencies=[Depends(LoginService.get_current_user)]
)


@OcrConfigController.get("/list", response_model=PageResponseModel)
async def get_tool_OcrConfig_list(
    request: Request,
    OcrConfig_page_query: OcrConfigPageQueryModel = Depends(
        OcrConfigPageQueryModel.as_query
    ),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    OcrConfig_page_query_result = await OcrConfigService.get_OcrConfig_list_services(
        query_db, OcrConfig_page_query, is_page=True
    )
    logger.info("获取成功")

    return ResponseUtil.success(model_content=OcrConfig_page_query_result)


@OcrConfigController.post(
    "", dependencies=[Depends(CheckUserInterfaceAuth("tool:OcrConfig:add"))]
)
@ValidateFields(validate_model="add_OcrConfig")
@Log(title="OCR配置", business_type=BusinessType.INSERT)
async def add_tool_OcrConfig(
    request: Request,
    add_OcrConfig: AddOcrConfigModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_OcrConfig_result = await OcrConfigService.add_OcrConfig_services(
        query_db, add_OcrConfig
    )
    logger.info(add_OcrConfig_result.message)
    return ResponseUtil.success(msg=add_OcrConfig_result.message)


@OcrConfigController.put(
    "", dependencies=[Depends(CheckUserInterfaceAuth("tool:OcrConfig:edit"))]
)
@ValidateFields(validate_model="edit_OcrConfig")
@Log(title="OCR配置", business_type=BusinessType.UPDATE)
async def edit_tool_OcrConfig(
    request: Request,
    edit_OcrConfig: EditOcrConfigModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    # 转换为OcrConfigModel用于数据库操作
    edit_OcrConfig_result = await OcrConfigService.edit_OcrConfig_services(
        query_db, edit_OcrConfig
    )
    logger.info(edit_OcrConfig_result.message)

    return ResponseUtil.success(msg=edit_OcrConfig_result.message)


@OcrConfigController.delete(
    "/{ids}", dependencies=[Depends(CheckUserInterfaceAuth("tool:OcrConfig:remove"))]
)
@Log(title="OCR配置", business_type=BusinessType.DELETE)
async def delete_tool_OcrConfig(
    request: Request, ids: str, query_db: AsyncSession = Depends(get_db)
):
    delete_OcrConfig = DeleteOcrConfigModel(ids=ids)
    delete_OcrConfig_result = await OcrConfigService.delete_OcrConfig_services(
        query_db, delete_OcrConfig
    )
    logger.info(delete_OcrConfig_result.message)

    return ResponseUtil.success(msg=delete_OcrConfig_result.message)


@OcrConfigController.get(
    "/{id}",
    response_model=OcrConfigModel,
    dependencies=[Depends(CheckUserInterfaceAuth("tool:OcrConfig:query"))],
)
async def query_detail_tool_OcrConfig(
    request: Request, id: int, query_db: AsyncSession = Depends(get_db)
):
    OcrConfig_detail_result = await OcrConfigService.OcrConfig_detail_services(
        query_db, id
    )
    logger.info(f"获取id为{id}的信息成功")

    return ResponseUtil.success(data=OcrConfig_detail_result)


@OcrConfigController.post(
    "/export", dependencies=[Depends(CheckUserInterfaceAuth("tool:OcrConfig:export"))]
)
@Log(title="OCR配置", business_type=BusinessType.EXPORT)
async def export_tool_OcrConfig_list(
    request: Request,
    OcrConfig_page_query: OcrConfigPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    OcrConfig_query_result = await OcrConfigService.get_OcrConfig_list_services(
        query_db, OcrConfig_page_query, is_page=False
    )
    OcrConfig_export_result = await OcrConfigService.export_OcrConfig_list_services(
        OcrConfig_query_result
    )
    logger.info("导出成功")

    return ResponseUtil.streaming(data=bytes2file_response(OcrConfig_export_result))
